<?php
namespace app\index\controller;
use app\CommonController;
use app\index\middleware\Auth;
use app\model\Article;
use app\service\LetterService;

class Letter extends CommonController
{
    protected $middleware = [Auth::class];

    public function index()
    {
        if ($this->request->post()) {
            $letterService = $this->app->make(LetterService::class);
            $params["is_del"] =0;
            $params["member_id"] = $this->request->uid;
            $list =  $letterService->getListWithOutCount($params, "id,title,read_status,create_at");
            $content = $this->fetch("letter-ajax", ["list" => $list]);
            $this->result(["data" => $content]);
        } else {
            return $this->fetch("index");
        }
    }

    public function detail()
    {
        $letterService = $this->app->make(LetterService::class);
        $id = $this->request->param("id");
        if (!$id) {
            $this->error("参数错误");
        }
        $res = $letterService->getOne(["id"=>$id,"is_del"=>0],"*");
        if (empty($res)) {
            $this->error("站内信不存在");
        }
        if ($res->read_status==0) {
            $res->read_status=1;
            $res->save();
        }
        $this->assign("info", $res);
        return $this->fetch("detail");
    }
}